// secondary_page/propertyRepair/propertyRepair.js
import {getRepairType,addRepairInfo} from "../../apis/propertyRepair"
import type from '../../conmon/constants'
import {tips} from '../../conmon/common'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
    currenuIndex:0,
    fileList:[ //图片信息
    ],
    types:[
    ],
    rtId:0,//类型id
    content:"",//内容,
    address:"",//报修地点
    ownername:"",//报修人名
    phonenum:"",//电话号码
  },
  clickTypes(event){
    console.log(event);
    const index = event.currentTarget.dataset.index;
    const rtId = event.currentTarget.dataset.rtid;
       this.setData({
        currenuIndex : index,
        rtId:ruId
       })
   
  },
  afterRead(event) {
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    const that = this;
    wx.uploadFile({
      url: 'http://localhost/fileUpload/weixinUpload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: {"mkdirsName": 'repair' },
      success(res) {
        // console.log(res);
        // 上传完成需要更新 fileList
        const fileLists = that.data.fileList
        fileLists.push({ url: res.data });
        that.setData({ fileList:fileLists});
      },
    });
  },

  del(e){
      let list = this.data.fileList;
      for(var i=0;i<list.length;i++){
          if(list[i] == list[e.detail.index]){
            list.splice(i,1)
            this.setData({
              fileList:list
            })
          }
      }
      deleteUploadImg(e.detail.file.url,"repair").then((res)=>{
        console.log(res);
      })
  },
  onInput(event) {
    this.setData({
      currentDate: event.detail,
    });
  },
  //提交数据
  clickDta(){
      if(this.chick()){
        var rapairConfirmImage = [];
        for(var item in this.data.fileList){
          console.log(item);
          rapairConfirmImage.push(this.data.fileList[item].url)
        }

       const data =  {
          "communityId": 1,
          "ownerId": 1,
          "rapairAppointmentTime":new Date(this.data.currentDate) ,
          "rapairConfirmImage":rapairConfirmImage,
          "repairAddress": this.data.address,
          "repairContent":this.data.content,
          "repairId":'' ,
          "repairImage": [
            ""
          ],
          "repairOwnerName": this.data.ownername,
          "repairPhonenum": this.data.phonenum,
          "repairType": type.repairType.PAID,
          "rtId": this.data.rtId
        }
        addRepairInfo(data).then((res)=>{
          console.log(res);
        })
      }


  }, 
  //数据验证
  chick(){
    if(this.data.rtId == 0){
      tips("类型选择失败");
      return false;
    }
    if(this.data.content == null || this.data.content==""){
      tips("内容不能为空");
      return false;
    }
    if(this.data.address == null || this.data.address==""){
      tips("维修地址不能为空");
      return false;
    }
    if(this.data.ownername == null || this.data.ownername==""){
      tips("维修人姓名不能为空");
      return false;
    }
    const regex = /(13\d|14[579]|15[^4\D]|17[^49\D]|18\d)\d{8}/; 
    if(this.data.phonenum == null || this.data.phonenum==""){
      tips("电话号码不能为空");
      return false;
    }
    if(!regex.test(this.data.phonenum)){
      tips("手机号格式错误");
      return false;
    }
    return true;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getRepairType();
  },

  //获取报修类型
  getRepairType(){
    getRepairType().then((res)=>{
      console.log(res);
      if(res.code == 200){
          this.setData({
            types:res.obj,
            rtId:res.obj[0].rtId
          })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})